package com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.LogUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xiaoxing.zhinenghuacangku_nanjinglu.R;
import com.xiaoxing.zhinenghuacangku_nanjinglu.R2;
import com.xiaoxing.zhinenghuacangku_nanjinglu.di.component.DaggerNanJingLu_DianBiaoChuKuRenWuLieBiaoComponent;
import com.xiaoxing.zhinenghuacangku_nanjinglu.di.module.NanJingLu_DianBiaoChuKuRenWuLieBiaoModule;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.contract.NanJingLu_DianBiaoChuKuRenWuLieBiaoContract;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.presenter.NanJingLu_DianBiaoChuKuRenWuLieBiaoPresenter;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.adapter.NanJingLu_DianBiaoChuKuRenWuLieBiaoAdapter;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.entity.DianBiaoAgvYunHuiCangWei;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.entity.NanJingLu_DianBiaoChuKuRenWuLieBiao;
import com.xiaoxing.zhinenghuacangku_nanjinglu.mvp.ui.entity.XinyemianhuanchongweiBean;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import com.jess.arms.utils.CProgressDialogUtils;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonres.view.RecycleViewDivider;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.ZHI_NENG_HUA_CANG_KU_NANJINGLU_ACTIVITY_NAN_JING_LU__DIAN_BIAO_CHU_KU_REN_WU_LIE_BIAO)
public class NanJingLu_DianBiaoChuKuRenWuLieBiaoActivity extends BaseActivity<NanJingLu_DianBiaoChuKuRenWuLieBiaoPresenter> implements NanJingLu_DianBiaoChuKuRenWuLieBiaoContract.View, OnRefreshListener, NanJingLu_DianBiaoChuKuRenWuLieBiaoAdapter.INanJingLu_DianBiaoChuKuRenWuLieBiaoAdapter {


    private static boolean mIsNeedDemo = true;
    @BindView(R2.id.empty_text)
    TextView empty_text;
    @BindView(R2.id.empty_image)
    ImageView empty_image;

    @BindView(R2.id.empty)
    View mEmptyLayout;
    @BindView(R2.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R2.id.refreshLayout)
    RefreshLayout mRefreshLayout;
    @BindView(R2.id.btnRight)
    TextView btnRight;

    private NanJingLu_DianBiaoChuKuRenWuLieBiaoAdapter mAdapter;
    private List<NanJingLu_DianBiaoChuKuRenWuLieBiao> mDataBeanList = new ArrayList<>();
    private int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerNanJingLu_DianBiaoChuKuRenWuLieBiaoComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .nanJingLu_DianBiaoChuKuRenWuLieBiaoModule(new NanJingLu_DianBiaoChuKuRenWuLieBiaoModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_nan_jing_lu__dian_biao_chu_ku_ren_wu_lie_biao; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.activity_nan_jing_lu__dian_biao_chu_ku_ren_wu_lie_biao));

        try {
            btnRight.setVisibility(View.GONE);
            btnRight.setText("一键执行AGV退回命令");
            btnRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    StartAGVInstoreTaskQingDao();
                }
            });
            initRefreshLayout();
            initRecyclerView();
            initEmpty();
            //getNanJingLu_DianBiaoChuKuRenWuLieBiaoList();
            GetMeterOutStoreTask();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void StartAGVInstoreTaskQingDao() {
        showLoading();

        String methodNames = "StartAGVInstoreTaskQingDao";
        SoapParams paramss = null;
        paramss = new SoapParams();
        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodNames, paramss, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();
                try {

                    String result = object.getProperty(methodNames + "Result").toString();
                    if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                        showMessage("执行命令成功");
                    } else {
                        showMessage("执行命令失败");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
                showMessage("执行命令失败");

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
                showMessage("执行命令失败");

            }
        }, getAapiUserName(), getApiPwd());
    }

    private void initRefreshLayout() {
        mRefreshLayout.setRefreshHeader(new
                ClassicsHeader(this).setSpinnerStyle(SpinnerStyle.FixedBehind).setPrimaryColorId(R.color.public_colorPrimary).setAccentColorId(android.R.color.white));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.autoRefresh();
        mRefreshLayout.setEnableLoadMore(false);
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(NanJingLu_DianBiaoChuKuRenWuLieBiaoActivity.this));
        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 10, Color.parseColor("#EEEEEE")));

        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mAdapter = new NanJingLu_DianBiaoChuKuRenWuLieBiaoAdapter(NanJingLu_DianBiaoChuKuRenWuLieBiaoActivity.this, mDataBeanList, this));

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            }
        });
    }

    private void initEmpty() {
        empty_image.setImageResource(R.drawable.ic_empty);
        empty_text.setTextColor(Color.parseColor("#CFCFCF"));
        empty_text.setText("暂无数据");
    }

    private void GetMeterOutStoreTask() {
        GetMeterOutStoreTask(mSharedPreferencesHelper.getString(BaseConstants.UID));
    }

    @Override
    public void showLoading() {
        CProgressDialogUtils.showProgressDialog(this);
    }

    /**
     * @param
     * @return
     * @method
     * @description 电表出库任务列表
     * @date: 2020/6/15 0015 16:42
     * @author: 小星
     */
    public void GetMeterOutStoreTask(String uid) {
        mDataBeanList.clear();
        mAdapter.notifyDataSetChanged();
        mEmptyLayout.setVisibility(View.VISIBLE);
        mRecyclerView.setVisibility(View.GONE);

        showLoading();
        String methodName = "GetMeterOutStoreTask";
        SoapParams params = new SoapParams();
        params.put("uid", uid);

        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                try {
                    String result = object.getProperty(methodName + "Result").toString();


                    if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100") && !result.equals("false")) {


                        List<NanJingLu_DianBiaoChuKuRenWuLieBiao> nanJingLu_dianBiaoChuKuRenWuLieBiaos = new Gson().fromJson(result, new TypeToken<List<NanJingLu_DianBiaoChuKuRenWuLieBiao>>() {
                        }.getType());

                        if (nanJingLu_dianBiaoChuKuRenWuLieBiaos != null && nanJingLu_dianBiaoChuKuRenWuLieBiaos.size() > 0) {
                            mDataBeanList.clear();
                            mDataBeanList.addAll(nanJingLu_dianBiaoChuKuRenWuLieBiaos);
                            mAdapter.notifyDataSetChanged();
                            mEmptyLayout.setVisibility(View.GONE);
                            mRecyclerView.setVisibility(View.VISIBLE);
                        } else {
                            mEmptyLayout.setVisibility(View.VISIBLE);
                            mRecyclerView.setVisibility(View.GONE);
                        }
                    } else {
                    }

                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
                showMessage(content);
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
                showMessage(fault.getMessage());

            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    public void hideLoading() {
        CProgressDialogUtils.cancelProgressDialog(this);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this, message);

    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        //getNanJingLu_DianBiaoChuKuRenWuLieBiaoList();
        mRefreshLayout.finishRefresh();
        mEmptyLayout.setVisibility(View.GONE);

    }

    @Override
    public void getNanJingLu_DianBiaoChuKuRenWuLieBiaoDataSuccess(NanJingLu_DianBiaoChuKuRenWuLieBiao entityList) {

//        if (entityList != null && entityList.getData() != null && entityList.getData().size() > 0) {
//            mDataBeanList.clear();
//            mDataBeanList.addAll(entityList.getData());
//            mAdapter.notifyDataSetChanged();
//            mEmptyLayout.setVisibility(View.GONE);
//            mRecyclerView.setVisibility(View.VISIBLE);
//        } else {
//            mEmptyLayout.setVisibility(View.VISIBLE);
//            mRecyclerView.setVisibility(View.GONE);
//        }

    }

    private void getNanJingLu_DianBiaoChuKuRenWuLieBiaoList() {
        HashMap<String, String> map = new HashMap<>();

        mPresenter.getNanJingLu_DianBiaoChuKuRenWuLieBiaoList(map);
    }

    /**
     * @param
     * @return
     * @method
     * @description AGV确认出库
     * @date: 2020/6/15 0015 16:53
     * @author: 小星
     */
    public void ConfirmOutMeterAgv(String agvtaskid) {
        showLoading();
        String methodName = "ConfirmOutMeterAgv";
        SoapParams params = new SoapParams();
        params.put("agvtaskid", agvtaskid);

        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                try {
                    String result = object.getProperty(methodName + "Result").toString();
                    if (result.equals("True")) {
                        showMessage("确认成功");
                    } else {
                        showMessage("确认失败");
                    }

                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
                showMessage(content);
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
                showMessage(fault.getMessage());

            }
        }, getAapiUserName(), getApiPwd());
    }

    /**
     * @param
     * @return
     * @method
     * @description 立库确认出库
     * @date: 2020/6/15 0015 16:27
     * @author: 小星
     */
    public void ConfirmOutMeterLiKu(String likutaskid) {
        showLoading();
        String methodName = "ConfirmOutMeterLiKu";
        SoapParams params = new SoapParams();
        params.put("likutaskid", likutaskid);

        SoapUtil.getInstance(this).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                try {
                    String result = object.getProperty(methodName + "Result").toString();
                    if (result.equals("True")) {
                        showMessage("确认成功");
                    } else {
                        showMessage("确认失败");
                    }

                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();
                showMessage(content);
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();
                showMessage(fault.getMessage());

            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    public void queRenTuiHui(NanJingLu_DianBiaoChuKuRenWuLieBiao item) {

        showConfirmMessageTuiHuiShuLiangDialog(item.getReturnnum(), item);

    }

    @Override
    public void queRenChuKu(NanJingLu_DianBiaoChuKuRenWuLieBiao item) {
        OutMeterTaskConfirm(item.getBid());
    }

    public void OutMeterTaskConfirm(String bid) {
        showLoading();
        String methodNames = "OutMeterTaskConfirm";

        SoapParams params = new SoapParams();
        params.put("bid", bid);

        String finalMethodNames = methodNames;
        SoapUtil.getInstance(getContext()).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodNames, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(finalMethodNames + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    try {
                        GetMeterOutStoreTask();

                        if (Integer.parseInt(result) > 0) {
                            showMessage("命令执行成功");
                        } else {
                            showMessage("命令执行失败");
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    showMessage("命令执行失败");
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();

            }
        }, getAapiUserName(), getApiPwd());
    }

    private void showConfirmMessageTuiHuiShuLiangDialog(String num, NanJingLu_DianBiaoChuKuRenWuLieBiao item) {
        new QMUIDialog.MessageDialogBuilder(this)
                .setTitle("确认需退回数量?")
                .setMessage("需退回数量： " + num)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        GetUserSpaceListForAutoStoreQingDao(mSharedPreferencesHelper.getString(BaseConstants.UID), "", "1", item.getReturnnum(), item.getBid(), item);
                        dialog.dismiss();
                    }
                })
                .create(mCurrentDialogStyle).show();
    }

    /**
     * @param
     * @return
     * @method
     * @description 确认退回, 查询仓位
     * @date: 2020/6/15 0015 16:56
     * @author: 小星
     */
    public void GetUserSpaceListForAutoStoreQingDao(String user_id, String spname, String ismeter, String returnnum, String bid, NanJingLu_DianBiaoChuKuRenWuLieBiao item) {
        showLoading();
        String methodNames = "GetUserSpaceListForAutoStoreQingDao";

        SoapParams params = new SoapParams();
        params.put("user_id", user_id);
//        params.put("spname", spname); //暂时为空，不传
        params.put("ismeter", ismeter);

        String finalMethodNames = methodNames;
        SoapUtil.getInstance(getContext()).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodNames, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(finalMethodNames + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    try {

                        List<DianBiaoAgvYunHuiCangWei> dianBiaoAgvYunHuiCangWeis = ArmsUtils.parseString2List(result, DianBiaoAgvYunHuiCangWei.class);
                        showXuanZeTuiHuiCangWeiDialog(dianBiaoAgvYunHuiCangWeis, returnnum, bid, item);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    showMessage("没有出库位可选");
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();

            }
        }, getAapiUserName(), getApiPwd());
    }

    /**
     * @param
     * @return
     * @method
     * @description 选择退回仓位弹出框
     * @date: 2020/6/15 0015 16:07
     * @author: 小星
     */
    public void showXuanZeTuiHuiCangWeiDialog(List<DianBiaoAgvYunHuiCangWei> dianBiaoAgvYunHuiCangWeis, String returnnum, String bid, NanJingLu_DianBiaoChuKuRenWuLieBiao item) {

        ArrayList<String> list = new ArrayList<String>();
        HashMap<String, String> dataIds = new HashMap<>();

        for (int i = 0; i < dianBiaoAgvYunHuiCangWeis.size(); i++) {
            list.add(dianBiaoAgvYunHuiCangWeis.get(i).getLikuLineNumber() + "-" + dianBiaoAgvYunHuiCangWeis.get(i).getSpname());
            dataIds.put(dianBiaoAgvYunHuiCangWeis.get(i).getLikuLineNumber() + "-" + dianBiaoAgvYunHuiCangWeis.get(i).getSpname(), dianBiaoAgvYunHuiCangWeis.get(i).getSpid());
        }

        String[] items = new String[list.size()];
        list.toArray(items);

        QMUIDialog.CheckableDialogBuilder builder = new QMUIDialog.CheckableDialogBuilder(getContext())
                .setCheckedIndex(0)
                .setTitle("选择退回仓位")
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

        builder.addAction("取消", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                dialog.dismiss();
            }
        });
        builder.addAction("提交", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                String selectedSpid = dataIds.get(items[builder.getCheckedIndex()]);
                LogUtils.debugEInfo("selectedSpid=" + selectedSpid);
                showTianXieShuLiangEditTextDialog(returnnum, selectedSpid, bid, item);
                dialog.dismiss();

            }
        });
        builder.create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show();
    }

    /**
     * @param
     * @return
     * @method
     * @description 选择仓位后，填写确认数量
     * @date: 2020/6/16 0016 9:02
     * @author: 小星
     */
    private void showTianXieShuLiangEditTextDialog(String returnnum, String spid, String bid, NanJingLu_DianBiaoChuKuRenWuLieBiao item) {
        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(getContext());
        builder.setTitle("数量")
                .setPlaceholder("在此输入数量")
                .setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
                .setDefaultText(returnnum)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("确定", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 0) {

                            //1 agv  2--立库
                            if (item.getMachinetype().equals("1")) { //AGV 查询缓冲位置
                                getTuiHuiChongWei("0", bid, mSharedPreferencesHelper.getString(BaseConstants.UID), text.toString(), spid, "");
                            } else if (item.getMachinetype().equals("2")) { //立库 不需要缓冲位置，直接执行退回操作
                                OutMeterTaskReturn(bid, "0", mSharedPreferencesHelper.getString(BaseConstants.UID), text.toString(), spid, "");
                            }


                            dialog.dismiss();
                        } else {
                            showMessage("请填写数量");
                        }
                    }
                })
                .create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show();
    }

    /**
     * @param out_storageList_id 默认为0 仅限青岛南京路仓库-电表
     * @return
     * @method
     * @description 出库缓冲位置
     * @date: 2020/6/15 0015 15:57
     * @author: 小星
     */
    public void getTuiHuiChongWei(String out_storageList_id, String bid, String uid, String num, String spid, String remark) {
        showLoading();
        String methodNames = "GetBatchFreeOutStoreBuffer";

        SoapParams params = new SoapParams();
        params.put("out_storagelistId", out_storageList_id);

        String finalMethodNames = methodNames;
        SoapUtil.getInstance(getContext()).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodNames, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(finalMethodNames + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    try {

                        List<XinyemianhuanchongweiBean> xinyemianhuanchongweiBeanList = ArmsUtils.parseString2List(result, XinyemianhuanchongweiBean.class);
                        showXuanZeTuiHuiCangWeiDialog(xinyemianhuanchongweiBeanList, bid, uid, num, spid, remark);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    showMessage("没有出库位可选");
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();

            }
        }, getAapiUserName(), getApiPwd());
    }

    public void showXuanZeTuiHuiCangWeiDialog(List<XinyemianhuanchongweiBean> xinyemianhuanchongweiBeans, String bid, String uid, String num, String spid, String remark) {

        ArrayList<String> list = new ArrayList<String>();
        HashMap<String, String> dataIds = new HashMap<>();

        for (int i = 0; i < xinyemianhuanchongweiBeans.size(); i++) {
            list.add(xinyemianhuanchongweiBeans.get(i).getBuffer_name());
            dataIds.put(xinyemianhuanchongweiBeans.get(i).getBuffer_name(), xinyemianhuanchongweiBeans.get(i).getId());
        }

        String[] items = new String[list.size()];
        list.toArray(items);

        QMUIDialog.CheckableDialogBuilder builder = new QMUIDialog.CheckableDialogBuilder(getContext())
                .setCheckedIndex(0)
                .setTitle("选择退库位置")
                .addItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

        builder.addAction("取消", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {
                dialog.dismiss();
            }
        });
        builder.addAction("提交", new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog dialog, int index) {

                try {
                    String selectedId = dataIds.get(items[builder.getCheckedIndex()]);
                    LogUtils.debugEInfo("selectedId=" + selectedId);
                    OutMeterTaskReturn(bid, selectedId, uid, num, spid, "");

                } catch (Exception e) {
                    e.printStackTrace();
                }
                dialog.dismiss();

            }
        });
        builder.create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show();
    }

    /**
     * @param
     * @return
     * @method
     * @description 确认退回
     * @date: 2020/6/15 0015 16:59
     * @author: 小星
     */
    public void OutMeterTaskReturn(String bid, String outbufferid, String uid, String num, String spid, String remark) {
        showLoading();
        String methodNames = "OutMeterTaskReturn";

        SoapParams params = new SoapParams();
        params.put("bid", bid);
        params.put("outbufferid", outbufferid);
        params.put("uid", uid);
        params.put("num", num);
        params.put("spid", spid);
        params.put("remark", remark);

        String finalMethodNames = methodNames;
        SoapUtil.getInstance(getContext()).callEntry(getUrl(), BaseConstants.NAME_SPACE, methodNames, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                hideLoading();

                String result = object.getProperty(finalMethodNames + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    try {
                        GetMeterOutStoreTask();

                        if (result.equals("true")) {
                            showMessage("命令执行成功");
                        } else {
                            showMessage("命令执行失败");
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    showMessage("命令执行失败");
                }
            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                hideLoading();

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                hideLoading();

            }
        }, getAapiUserName(), getApiPwd());
    }

}
